import java.util.ArrayList;
import java.util.List;

/**
 * @author LKQ
 * @date 2022/1/12 17:07
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[][] firstList = {{5, 10}}, secondList = {{3, 10}};
        solution.intervalIntersection(firstList, secondList);
    }
    public int[][] intervalIntersection(int[][] A, int[][] B) {
        List<int[]> ans = new ArrayList();
        int i = 0, j = 0;
        while (i < A.length && j < B.length) {
            // 两个指针分别指向对应的列表，lo为左端点最大的，hi为右端点最小的
            int lo = Math.max(A[i][0], B[j][0]);
            int hi = Math.min(A[i][1], B[j][1]);
            if (lo <= hi) {
                ans.add(new int[]{lo, hi});
            }
            // 移除最小的左端点
            if (A[i][1] < B[j][1]) {
                i++;
            } else {
                j++;
            }
        }

        return ans.toArray(new int[ans.size()][]);
    }

}
